<template>
	<rui-pages>
		<rui-chat-header @side="openSide"></rui-chat-header>
		<rui-chat-message></rui-chat-message>
		<rui-chat-textarea @change="add" @side="openSide"></rui-chat-textarea>
		<rui-chat-side ref="refSide"></rui-chat-side>
	</rui-pages>
</template>

<script setup>
	import {
		ref,
		nextTick
	} from "vue"
	import {
		onLoad
	} from "@dcloudio/uni-app"
	import Push from "@/util/Push.js"
	import {
		msgStore,
		userStore
	} from "@/store/index.js"

	let refSide = ref();

	const msg = msgStore();
	const push = new Push();
	const user = userStore();

	push.start()

	onLoad(() => {
		scrollToBottom()

	})
	
	uni.$on('message', (data) => {
	  	setTimeout(() => {
	  		msg.add(data)
	  		scrollToBottom();
	  	}, 500)
	});
	
	// push.watch(data => {
	// 	if (data.type == 'updateUser') {
	// 		user.setInfo(data.data)
	// 	}
	// 	setTimeout(() => {
	// 		msg.add(data)
	// 		scrollToBottom();
	// 	}, 1000)
	// })
	const openSide = () => {
		refSide.value.open()
	}

	const scrollToBottom = () => {
		nextTick(() => {
			uni.pageScrollTo({
				scrollTop: 20000000,
				duration: 0
			});
		});
	}

	const add = (content) => {
		msg.add({
			content,
			type: 'search',
			from: 'me',
		})
		push.send(content)
		scrollToBottom();
	}
</script>

<style lang="scss" scoped>

</style>